package com.wu.wechat.plugins;


import cn.hutool.core.util.RandomUtil;
import com.wu.wechat.util.DataDictTool;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.DateTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Enumeration;
import java.util.Properties;

public class ViewVelocity implements InitializingBean {
    static final Logger logger = LoggerFactory.getLogger(ViewVelocity.class);
    private static final String PAGE_ROOT = "content/";
    private final String propertiesFile = "velocity.properties";
    public static final boolean viewhtmlflag;

    public ViewVelocity() {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            Properties p = new Properties();
            p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties"));
            Velocity.init(p);
            if (logger.isDebugEnabled()) {
                logger.debug("Loading VelocityEngine....");
                Enumeration en = p.propertyNames();

                while(en.hasMoreElements()) {
                    String key = en.nextElement().toString();
                    logger.debug("\t" + key + "=" + p.getProperty(key));
                }
            }

        } catch (Exception var4) {
            logger.error("启动Velocity失败");
            throw new Exception("启动Velocity失败", var4);
        }
    }

    public static void view(HttpServletResponse response, String template) throws Exception {
        view(response, template, (VelocityContext)null);
    }

    /** @deprecated */
    @Deprecated
    public static void view(HttpServletResponse response, String template, VelocityContext velocityContext) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Velocity loading：" + template);
        }

        HttpServletRequest request = ContextHolderUtils.getRequest();
        if (velocityContext == null) {
            velocityContext = new VelocityContext();
        }

        velocityContext.put("Format", new SimpleFormat());
        velocityContext.put("dictTool", new DataDictTool());
        velocityContext.put("dateTool", new DateTool());
        String basePath = request.getContextPath();
        logger.debug("---------------basePath--------------" + basePath);
        velocityContext.put("basePath", basePath);
        StringWriter writer = new StringWriter();
        Velocity.mergeTemplate("content/" + template, "UTF-8", velocityContext, writer);
        outputToPage(request, response, writer.toString());
    }

    public static void view(HttpServletRequest request, HttpServletResponse response, String template, VelocityContext velocityContext) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("Velocity loading：" + template);
        }

        if (velocityContext == null) {
            velocityContext = new VelocityContext();
        }

        velocityContext.put("Format", new SimpleFormat());
        velocityContext.put("dictTool", new DataDictTool());
        velocityContext.put("dateTool", new DateTool());
        String basePath = request.getContextPath();
        logger.debug("---------------basePath--------------" + basePath);
        velocityContext.put("basePath", basePath);
        StringWriter writer = new StringWriter();
        Velocity.mergeTemplate("content/" + template, "UTF-8", velocityContext, writer);
        outputToPage(request, response, writer.toString());
    }

    private static void outputToPage(HttpServletRequest request, HttpServletResponse response, String content) {
        response.setContentType("text/html");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;

        try {
            if (!response.isCommitted()) {
                writer = response.getWriter();
                writer.println(content);
                writer.flush();
                writer.close();
            }
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception var12) {
            }

        }

    }

    static {
        viewhtmlflag = RandomUtil.randomBoolean();
    }
}

